package com.rchat.pocmini.db;


import android.content.Context;

import com.airtalkee.sdk.entity.AirMessage;

import java.util.List;

/**
 * 内存数据管理
 *
 * @author sienna
 */
public class MemoryDataStoreModel {
    private Context mContext;

    //    private IChannelDao ChannelDao;
    private MessageDao MessageDao;
//    private ISessionDao SessionDao;
//    private ISessionMemberDao SessionMemberDao;
//    private IBroadcastDao BroadcastDao;

//    private List<PocAirChannel> pocAirChannels = new ArrayList<PocAirChannel>();

//    //==================频道记录操作开始==================
//    public List<PocAirChannel> getChannelInfo(){
//        if (pocAirChannels.isEmpty()) {
//            tryInitChannelDao();
//            pocAirChannels.clear();
//            pocAirChannels.addAll(ChannelDao.loadChannelInfo());
//            Collections.sort(pocAirChannels);
//        }
//        return pocAirChannels;
//    }
//
//    public List<PocAirChannel> getChannelInfoFromDb(){
//        tryInitChannelDao();
//        pocAirChannels.clear();
//        pocAirChannels.addAll(ChannelDao.loadChannelInfo());
//        return pocAirChannels;
//    }
//
//    public PocAirChannel getChannelInfoById(String id){
//        tryInitChannelDao();
//        return ChannelDao.loadChannelInfo(id);
//    }
//
//    /**
//     * 更新频道数据
//     * @param Channels
//     */
//    public void updateChannelInfo(List<AirChannel> Channels){
//        tryInitChannelDao();
//        ChannelDao.updateChannelInfo(Channels);
//    }
//
//    /**
//     * 去重的联系人
//     * @return
//     */
//    public List<AirContact> getAllChannelMembers(){
//        List<AirContact> airContacts = PocChannelManager.getInstance().getAllChannelMembers();
//        for(int i=0; i< airContacts.size()-1 ; i++ )   {
//            for(int j = airContacts.size() - 1 ; j> i; j-- )   {
//                if(airContacts.get(j).getIpocId().equals(airContacts.get(i).getIpocId()))   {
//                    airContacts.remove(j);
//                }
//            }
//        }
//        return airContacts;
//    }
//
//    /**
//     * 获取频道数据
//     * @return
//     */
//    public List<PocAirChannel> loadChannelInfo(){
//        tryInitChannelDao();
//        return ChannelDao.loadChannelInfo();
//    }
//
//    public void updateChannelUnReadCount(String cid, boolean isAdd){
//        tryInitChannelDao();
//        ChannelDao.updateUnReadCount(cid, isAdd);
//    }
//
//    public void channelAppend(AirChannel channel){
//        tryInitChannelDao();
//        ChannelDao.channelAppend(channel);
//    }
//
//    public void deleteChannel(String channelId){
//        tryInitChannelDao();
//        ChannelDao.deleteChannel(channelId);
//    }

    public static MemoryDataStoreModel get(Context context) {
        return BeanManager.get().getBean(MemoryDataStoreModel.class, context);
    }

    private MemoryDataStoreModel(Context context) {
        mContext = context;
    }

//    private void tryInitChannelDao(){
//        if(ChannelDao == null){
//            synchronized (this) {
//                if(ChannelDao == null){
//                    ChannelDao = BeanManager.get().getBean(ChannelDaoImpl.class, mContext);
//                }
//            }
//        }
//    }

    //==================消息记录操作开始==================
    public int getAllMessagesCount(String sid) {
        tryInitMessageDao();
        return MessageDao.getAllMessagesCount(sid);
    }

    /**
     * @param sid         sesioncode
     * @param currentPage 当前页
     * @param pageSize    每页显示的记录
     * @return
     */
    public List<AirMessage> loadMessageInfos(String sid, int currentPage, int pageSize) {
        tryInitMessageDao();
        return MessageDao.loadMessageInfos(sid, currentPage, pageSize);
    }

    public void deleteMessage(String messageId) {
        tryInitMessageDao();
        MessageDao.deleteMessage(messageId);
    }

    public void insertMessage(AirMessage message) {
        tryInitMessageDao();
        MessageDao.insertMessage(message);
    }

    public void batchInsertMessage(List<AirMessage> messages) {
        tryInitMessageDao();
        MessageDao.batchInsertMessage(messages);
    }

    public void updateMessage(AirMessage message) {
        tryInitMessageDao();
        MessageDao.updateMessage(message);
    }

    public void cleanMessageTb() {
        tryInitMessageDao();
        MessageDao.cleanTable();
    }

//    public List<AirMessage> loadPttMessageInfos(String sid){
//        tryInitMessageDao();
//        return MessageDao.loadPttMessageInfos(sid, 1, AppConfig.DFL_PTT_MSG_LOAD);
//    }
//
//    public List<AirMessage> loadPttMessageInfos(String sid, int offset){
//        tryInitMessageDao();
//        return MessageDao.loadPttMessageInfos(sid, offset);
//    }

    private void tryInitMessageDao() {
        if (MessageDao == null) {
            synchronized (this) {
                if (MessageDao == null) {
                    MessageDao = BeanManager.get().getBean(MessageDaoImpl.class, mContext);
                }
            }
        }
    }

    //    //==================临时会话操作开始==================
//    public void sessionAppend(AirSession airSession){
//        tryInitSessionDao();
//        SessionDao.sessionAppend(airSession);
//    }
//
//    public void sessionClean(){
//        tryInitSessionDao();
//        SessionDao.sessionClean();
//    }
//
//    public void sessionCleanUnread(String sessionCode){
//        tryInitSessionDao();
//        SessionDao.sessionCleanUnread(sessionCode);
//    }
//
//    public void sessionDelete(String sessionCode){
//        tryInitSessionDao();
//        SessionDao.sessionDelete(sessionCode);
//    }
//
//    public List<AirSession> sessionLoad(){
//        tryInitSessionDao();
//        return SessionDao.sessionLoad();
//    }
//
//    public AirSession loadSesionInfo(String sid){
//        tryInitSessionDao();
//        return SessionDao.loadSesionInfo(sid);
//    }
//
//    public void sessionUpdate(String sessionCode, AirSession airSession){
//        tryInitSessionDao();
//        SessionDao.sessionUpdate(sessionCode, airSession);
//    }
//
//    public void updateSessionUnReadCount(String sid, boolean isAdd){
//        tryInitChannelDao();
//        ChannelDao.updateUnReadCount(sid, isAdd);
//    }
//
//    private void tryInitSessionDao(){
//        if(SessionDao == null){
//            synchronized (this) {
//                if(SessionDao == null){
//                    SessionDao = BeanManager.get().getBean(SessionDaoImpl.class, mContext);
//                }
//            }
//        }
//    }
//
//    public List<AirContact> sessionMemberLoad(String sessionCode){
//        tryInitSessionMemberDao();
//        return SessionMemberDao.sessionMemberLoad(sessionCode);
//    }
//
//    private void tryInitSessionMemberDao(){
//        if(SessionMemberDao == null){
//            synchronized (this) {
//                if(SessionMemberDao == null){
//                    SessionMemberDao = BeanManager.get().getBean(SessionMemberDaoImpl.class, mContext);
//                }
//            }
//        }
//    }
//
//    //==================广播消息操作开始==================
//    public List<PocBroadcastMsg> loadBroadcastMsgs(){
//        tryInitBroadcastMsgDao();
//        return BroadcastDao.loadBroadcastMsgs();
//    }
//
//    public PocBroadcastMsg loadBroadcastMsg(String bdId){
//        tryInitBroadcastMsgDao();
//        return BroadcastDao.loadBroadcastMsg(bdId);
//    }
//
//    public void broadcastMsgAppend(PocBroadcastMsg pocBroadcastMsg){
//        tryInitBroadcastMsgDao();
//        BroadcastDao.broadcastMsgAppend(pocBroadcastMsg);
//    }
//
//    public void deleteBroadcastMsg(String bdId){
//        tryInitBroadcastMsgDao();
//        BroadcastDao.deleteBroadcastMsg(bdId);
//    }
//
//    public void updateBroadcastMsgReadSate(String bdId, boolean isRead){
//        tryInitBroadcastMsgDao();
//        BroadcastDao.updateReadSate(bdId, isRead);
//    }
//
//    private void tryInitBroadcastMsgDao(){
//        if(BroadcastDao == null){
//            synchronized (this) {
//                if(BroadcastDao == null){
//                    BroadcastDao = BeanManager.get().getBean(BroadcastDaoImpl.class, mContext);
//                }
//            }
//        }
//    }
//
    public static class Creator implements BeanManager.Creator<MemoryDataStoreModel> {
        @Override
        public boolean isSingleton() {
            return true;
        }

        @Override
        public MemoryDataStoreModel create(Context context) {
            return new MemoryDataStoreModel(context);
        }
    }

    //
    public void cleanData() {
//        if (ChannelDao != null) {
//            ChannelDao.closeDB();
//        }
        if (MessageDao != null) {
            MessageDao.closeDB();
        }
//        if (SessionDao != null) {
//            SessionDao.closeDB();
//        }
//        if (SessionMemberDao != null) {
//            SessionMemberDao.closeDB();
//        }
//        if (BroadcastDao != null) {
//            BroadcastDao.closeDB();
//        }
    }
}

